const BaseHandler     = require('../base-handler');
const HTTP_VERBS      = require('../../../constants').HTTP_VERBS;

class PhotoCommentsHandler extends BaseHandler {
    get pattern() {
        return '/photos/:asset_id/comments';
    }

    doPOST(request, response) {
        var me = this;
        var submitData = {};
        submitData['comment'] = request.body;
        submitData['asset_id'] = request.params.asset_id;
        var wallUserId = request.session.walls[submitData.comment.wall_id].wall_user_id;
        this.Controller.getWallUserProfile(wallUserId, (result) => {
            if(result.success) {
                submitData['user_id'] = result.data._id;
                submitData['name'] = result.data.name;
                submitData['profile_image'] = result.data.profile_image;
            } else {
                response.end(JSON.stringify(result));
            }
            me.Controller.saveAssetComment(submitData, (result) => {
                response.json(result);
            });
        });
    }

    doGET(request, response){
        var assetId = request.params.asset_id;
        var count = request.query.count || 2;
        var type = request.query.type || 'old';
        var since = request.query.since || 'now';
        var orderBy = request.query.orderBy || -1;
        this.Controller.getCommentsByAssetId(assetId, {
            count: count,
            type: type,
            since: since,
            orderBy: orderBy
        }, (result) => {
            response.json(result);
        });
    }

    //TODO: Should change the pattern to /assets/:asset_id/comments/:comment_id
    doDELETE(request, response) {
        var commentId = request.body.comment_id;
        this.Controller.removeCommentById(commentId, (result) => {
            response.json(result);
        });
    }

    setup() {
        this.registerVerb(HTTP_VERBS.DELETE, this.doDELETE);
        this.registerVerb(HTTP_VERBS.POST, this.doPOST);
        this.registerVerb(HTTP_VERBS.GET, this.doGET);
    }
}

module.exports = PhotoCommentsHandler;